function validateForm_subform() {
	var form = document.getElementById("subform");
	clearErrorMessages();
	//clearErrorLabels(form);
	var errors = false;
	var field;
	var lbErr;
	
	//field name: city
	//validator name: required
	if (cityRequired(false))
	    errors = true;
	
	//field name: phone
	//validator name: regex
	if (phoneValid(false))
	    errors = true;
	
	// field name: email
	// validator name: requiredstring
	if (emailRequired(false))
	    errors = true;

	// field name: email
	// validator name: emailvalid
	if (emailValid(false))
	    errors = true;
	
	// field name: username
	// validator name: required
	if (usernameRequired(false))
	    errors = true;
	
	// field name: username
	// validator name: length
	if (usernameLength(false))
	    errors = true;
	
	// field name: password
	// validator name: required
	if (passwordRequired(false))
	    errors = true;
	
	// field name: password
	// validator name: length
	if (passwordLength(false))
	    errors = true;
	
	// field name: repassword
	// validator name: not match
	if (repasswordMatch(false))
	    errors = true;

    // field name: credit
	// validator name: required
	if (creditRequired(false))
	    errors = true;

    // field name: gender
	// validator name: required
	if (genderRequired(false))
	    errors = true;

    // field name: security
	// validator name: required
	if (securityRequired(false))
	    errors = true;

	// field name: birthday
	// validator name: valid
	if (birthdayValid(false))
	    errors = true;
	
	return errors;
}

function cityRequired(contValid) {
    
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['city']) {
        field = form.elements['city'];
        lbErr = document.getElementById("lbErr_city");
        var error = "chưa chọn.";
        if (field.value == "--Hãy chọn thành phố--")
            errors = true;
    }
    if (!errors && contValid) {
        $("#city_ok_pic").css("display", "inline");
        $("#city_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#city_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function phoneValid(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['phone']) {
        field = form.elements['phone'];
        lbErr = document.getElementById("lbErr_phone");
        var error = "không phải số điện thoại";
        if (field.value != ""
				&& !field.value
						.match("(^[\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d]|^[\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d])"))
            errors = true;
        if (field.value == "")
            return false;
    }
    if (!errors && contValid) {
        $("#phone_ok_pic").css("display", "inline");
        $("#phone_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#phone_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function emailRequired(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['email']) {
        field = form.elements['email'];
        lbErr = document.getElementById("lbErr_email");
        var error = "chưa nhập.";
        if (field.value == "" || field.value.replace(/^\s+|\s+$/g, "").length == 0)
            errors = true;
        else
            return false;
    }
    if (!errors && contValid) {
        $("#email_ok_pic").css("display", "inline");
        $("#email_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#email_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function emailValid(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['email']) {
        field = form.elements['email'];
        lbErr = document.getElementById("lbErr_email");
        var error = "email không hợp lệ.";
        if (field.value.length > 0
				&& field.value
						.match(/\b(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b/gi) == null)
            errors = true;
        if (field.value == "")
            return false;
    }
    if (!errors && contValid) {
        $("#email_ok_pic").css("display", "inline");
        $("#email_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#email_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function usernameRequired(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['username']) {
        field = form.elements['username'];
        lbErr = document.getElementById("lbErr_username");
        var error = "chưa nhập.";
        if (field.value == "")
            errors = true;
        else
            return false;
    }
    if (!errors && contValid) {
        $("#username_ok_pic").css("display", "inline");
        $("#username_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#username_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function usernameLength(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['username']) {
        field = form.elements['username'];
        lbErr = document.getElementById("lbErr_username");
        var error = "tên đăng nhập từ 6~20 ký tự.";
        if (field.value.length < 6
				|| field.value.length > 20)
            errors = true;
    }
    if (!errors && contValid) {
        $("#username_ok_pic").css("display", "inline");
        $("#username_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#username_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function passwordRequired(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['password']) {
        field = form.elements['password'];
        lbErr = document.getElementById("lbErr_password");
        var error = "chưa nhập.";
        if (field.value == "")
            errors = true;
        else
            return false;
    }
    if (!errors && contValid) {
        $("#password_ok_pic").css("display", "inline");
        $("#password_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#password_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function passwordLength(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['password']) {
        field = form.elements['password'];
        lbErr = document.getElementById("lbErr_password");
        var error = "mật khẩu từ 6~12 ký tự.";
        if (field.value.length < 6
				|| field.value.length > 12)
            errors = true;
    }
    if (!errors && contValid) {
        $("#password_ok_pic").css("display", "inline");
        $("#password_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#password_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function repasswordMatch(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['repassword']) {
        field = form.elements['repassword'];
        lbErr = document.getElementById("lbErr_repassword");
        var error = "nhập lại mật khẩu không đúng.";
        if (field.value != form.elements['password'].value)
            errors = true;
        if (form.elements['password'].value == "" || form.elements['password'].value.length < 6
            || form.elements['password'].value.length > 12)
            return false;
    }
    if (!errors && contValid) {
        $("#repassword_ok_pic").css("display", "inline");
        $("#repassword_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#repassword_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function creditRequired(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['credit']) {
        field = form.elements['credit'];
        lbErr = document.getElementById("lbErr_credit");
        var error = "chưa nhập.";
        if (field.value == "")
            errors = true;
    }
    if (!errors && contValid) {
        $("#credit_ok_pic").css("display", "inline");
        $("#credit_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#credit_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function genderRequired(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['gender']) {
        field = form.elements['gender'];
        lbErr = document.getElementById("lbErr_gender");
        var error = "chưa chọn.";
        if (field.value == "--Hãy chọn giới tính--")
            errors = true;
    }
    if (!errors && contValid) {
        $("#gender_ok_pic").css("display", "inline");
        $("#gender_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if(errors) {
        $("#gender_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function securityRequired(contValid) {
    var form = document.getElementById("subform");
    var field;
    var lbErr;
    var errors = false;
    if (form.elements['security']) {
        field = form.elements['security'];
        lbErr = document.getElementById("lbErr_security");
        var error = "chưa nhập.";
        if (field.value == "")
            errors = true;
    }
    if (!errors && contValid) {
        $("#security_ok_pic").css("display", "inline");
        $("#security_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#security_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function birthdayValid(contValid) {
    var form = document.getElementById("subform");
    var day = form.elements['day'];
    var month = form.elements['month'];
    var year = form.elements['year'];
    var lbErr = document.getElementById("lbErr_birthday");
    var error;
    var errors = false;

    if (day.value == "" || month.value == "" || year.value == "") {
        errors = true;
        error = "chưa nhập"
    }
    else {
        var birthday = new Date(year.value, month.value - 1, day.value);
        var now = new Date();

        if (month.value > 12 || month.value < 1 || day.value < 1 || day.value > 31 || year.value < 0
        || (month.value - 1 != birthday.getMonth()) || now.getFullYear() < birthday.getFullYear()
        || (now.getMonth() < birthday.getMonth() && now.getFullYear() == birthday.getFullYear())
        || (now.getDate() < birthday.getDate() && now.getMonth() == birthday.getMonth() && now.getFullYear() == birthday.getFullYear())) {
            errors = true;
            error = "không hợp lệ";
        }
    }
    if (!errors && contValid) {
        $("#birthday_ok_pic").css("display", "inline");
        $("#birthday_ok_pic").fadeOut(4000);
        removeErrors(lbErr);
    }
    if (errors) {
        $("#birthday_ok_pic").css("display", "none");
        addErrors(lbErr, error);
    }
    return errors;
}

function addErrors(field, error) {
	field.innerHTML = error;
}

function removeErrors(field) {
    field.innerHTML = "";
}

function clearErrorMessages() {
	document.getElementById("lbErr_repassword").innerHTML="";
	document.getElementById("lbErr_password").innerHTML="";
	document.getElementById("lbErr_username").innerHTML="";
	document.getElementById("lbErr_email").innerHTML="";
	document.getElementById("lbErr_phone").innerHTML="";
	document.getElementById("lbErr_city").innerHTML="";
}

function blockPage() {
	$("#_isp-message_1").css("display","block");
}

function unblockPage() {
	$("#_isp-message_1").css("display","none");
}

function loadValidator() {
    $("#city").blur(function() {
        cityRequired(true);
    });

    $("#phone").blur(function() {
        phoneValid(true);
    });

    $("#email").blur(function() {
        emailRequired(true);
        emailValid(true);
    });

    $("#username").blur(function() {
        usernameRequired(true);
        usernameLength(true);
    });

    $("#password").blur(function() {
        passwordRequired(true);
        passwordLength(true);
    });

    $("#repassword").blur(function() {
        repasswordMatch(true);
    });

    $("#credit").blur(function() {
        creditRequired(true);
    });

    $("#gender").blur(function() {
        genderRequired(true);
    });

    $("#security").blur(function() {
        securityRequired(true);
    });

    $("#day").blur(function() {
        birthdayValid(true);
    });

    $("#month").blur(function() {
        birthdayValid(true);
    });

    $("#year").blur(function() {
        birthdayValid(true);
    });
}